var url = {
	add: api.role.add,
	edit: api.role.edit,
	delete: api.role.delete,
}
var setting = {
	check: {
		enable: true,
		chkDisabledInherit: true
	},
	data: {
		simpleData: {
			enable: true
		}
	}
};
;
{if $Think.get.id}
Load()
	 ajax(api.role.list, {
 		where_json: JSON.stringify({
 			role_id: ['in', "{$Think.get.id}"]
 		})
 	}, 'post').done(function(res1) {
 		Load(1)
 			console.log(res1)
 			for(var key in res1.list[0]) {
 				$('input[name=' + key + ']').val(res1.list[0][key])
 			}
{else/}
 {/if}	
//请求区域
var area_load= layer.load(0, {
		  shade: [0.1,'#fff'] //0.1透明度的白色背景
		});
ajax(api.area.list,{
		is_all:1
	},'post').done((res)=>{
		layer.close(area_load)
		console.log(res)
		let tree=[];
		tree=fortree(tree,res.data,1)
		function fortree(tree,data,type){
			for(var i=0;i<data.length;i++){
				var qz=''
				for(var ss=1;ss<type;ss++){
					qz+='├'
				}
				data[i].area_name2=qz+data[i].area_name
				tree.push(data[i])
				if(data[i].children.length>0){
					fortree(tree,data[i].children,type+1)
				}else{}
			}
			return tree
		}
		console.log(tree)
		let arealist=`<option>请选择区域</option>`
		for(var tree_i=0;tree_i<tree.length;tree_i++){
			{if $Think.get.id}
			if(tree[tree_i].area_id==res1.list[0].area_id){
				arealist+=`<option selected value='${tree[tree_i].area_id}'>${tree[tree_i].area_name2}</option>`
			}else{
				arealist+=`<option value='${tree[tree_i].area_id}'>${tree[tree_i].area_name2}</option>`
			}
			{else/}
			arealist+=`<option value='${tree[tree_i].area_id}'>${tree[tree_i].area_name2}</option>`
			{/if}
			
		}
		$('.area').html(arealist)
		form.render()
	}).fail(()=>{
		layer.close(area_load)
	})
 	
	 //请求菜单
var menu_load= layer.load(0, {
		  shade: [0.1,'#fff'] //0.1透明度的白色背景
	});
		{if $Think.get.id}
		var url11=api.role.menu
		{else/}
		var url11=api.menu.list
		{/if}
ajax(url11,{

	module_type:0,
	is_all:1
	{if $Think.get.id}
	,role_id:'{$Think.get.id}'
	{/if}
},'post').done((res)=>{
	console.log(res)
	layer.close(menu_load)
	// let arr=[];
	for(var menu_i=0;menu_i<res.data.length;menu_i++){
		res.data[menu_i].pId=res.data[menu_i].pid
	}
	console.log(res.data)
	
	$.fn.zTree.init($("#ztreeid"), setting, res.data);
	
}).fail(()=>{
	layer.close(menu_load)
})
{if $Think.get.id}
}).fail(function(err) {
	Err('请求失败');
	Load(1)
}); 
  {else/}
  {/if}



//var data1 = {}
//upImage(api.upimage.upimage, '#test1', '#img1', data1)
form.on('submit(ss1)', function(data) {
	var treeObj = $.fn.zTree.getZTreeObj("ztreeid");
	var nodes = treeObj.getNodes();
	console.log(nodes);
	var text = [];
	for(var i = 0; i < nodes.length; i++) {
		if(nodes[i].checked) {
			text.push(nodes[i].id)
		}
		if(nodes[i].children) {
			for(var j = 0; j < nodes[i].children.length; j++) {
				if(nodes[i].children[j].checked) {
					text.push(nodes[i].children[j].id)
				}
				if(nodes[i].children[j].children) {
					for(var z = 0; z < nodes[i].children[j].children.length; z++) {
						if(nodes[i].children[j].children[z].checked) {
							text.push(nodes[i].children[j].children[z].id)
						}
					}
				}
			}
		}
	}
	console.log(text);
	var textval = '';
	for(var ti = 0; ti < text.length; ti++) {
		if(ti == 0) {
			textval += text[ti]
		} else {
			textval += ("," + text[ti])
		}
	}
	//当前容器的全部表单字段，名值对形式：{name: value}
	data.field.menus = textval
	data.field.module_type = 0
	console.log(data.field)
 	{if $Think.get.id}
 	data.field.role_id='{$Think.get.id}'
	  var sumbit=url.edit
	  {else/}
	  var sumbit=url.add
	  {/if}
	  
		 	ajax(sumbit,data.field,'post').done(function(res) {
					if(res.code==1){
						Msg('编辑成功')
						{if $Think.get.id}
						window.history.go(0)
						 {else/}
						 window.location.href='/html/system/role'
						 {/if}
					}else{
						Err(res.msg)
					}
			}).fail(function(err) {
				Err('请求失败')
			});

	return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
});